<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <Table :tableHeader="tableHeader" :tableData="tableData" :outTableHeight="0" :isLoading="isLoading" @currentRow="currentRow" />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { reactive, ref } from 'vue'
import 'dayjs/locale/zh-cn'
import { selectSomeList, selectList } from '@/plugins/mixin'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'InventoryLocation'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '分仓库别',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入区位编码',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '区位',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入库存位置',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入位置说明',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '新增'
  },
  {
    type: 'button',
    checkboxLabel: '删除当前行'
  }
])
// 表格数据
const tableHeader = ref([
  {
    label: '分仓库别',
    prop: 'warehouseNo'
  },
  {
    label: '库存编码',
    prop: 'stkNo'
  },
  {
    label: '区位',
    prop: 'areaType'
  },
  {
    label: '库存位置',
    prop: 'areaNo'
  },
  {
    label: '位置说明',
    prop: 'remark'
  }
])
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '新增') {
    const res = await LoginWork.post<any, any>('/stock/insertHouse', {
      areaNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入库存位置'
      )?.inputValue,
      areaType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '区位')
        ?.selectValue,
      remark: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入位置说明'
      )?.inputValue,
      stkNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入区位编码'
      )?.inputValue,
      warehouseNo: formList.value.find(
        (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分仓库别'
      )?.selectValue
    })
    if (res.code === 200) {
      JMessage.success(res.message)
      formList.value.forEach((item: any) => {
        item.inputValue = ''
        item.selectValue = ''
        item.dateValue = ''
      })
    } else {
      JMessage.warning(res.message)
    }
  }
  if (content === '删除当前行') {
    const res = await LoginWork.post<any, any>('/stock/deleteHouse', currentTableRow.value)
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.warning(res.message)
    }
    getTableList()
  }
}
const tableData = ref([])
const isLoading = ref(true)

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/stock/wareHouseList', {
    areaNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入库存位置'
    )?.inputValue,
    areaType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '区位')
      ?.selectValue,
    remark: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入位置说明'
    )?.inputValue,
    stkNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入区位编码'
    )?.inputValue,
    warehouseNo: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分仓库别')
      ?.selectValue
  })
  isLoading.value = false
  tableData.value = tableDataList
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

// 获取下拉菜单
const getSelectList = () => {
  selectList(formList, () => service.get('/back/stockArea'))
}
onMounted(() => {
  getSelectList()
})
const currentTableRow = ref()
const currentRow = (row: any) => {
  currentTableRow.value = JSON.parse(JSON.stringify(row))
}
</script>

<style lang="less" scoped>
table {
  table-layout: auto !important;
}

:deep(.el-table__header) {
  width: 100% !important;
}

:deep(.el-table__body) {
  width: 100% !important;
}
</style>
